public class HashBuck {
    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val){
            this.key = key;
            this.val = val;
        }
    }

    private Node[] array;
    private int usedSize;

    public HashBuck(){
        this.array = new Node[10];
    }

    public void put(int key, int val){
        int index = key % this.array.length;
        Node cur = this.array[index];

        while(cur != null){
            if(cur.key == key){
                cur.val = val;
                return;
            }
            cur = cur.next;
        }

        Node node = new Node(key, val);
        node.next = this.array[index];
        this.array[index] = node;
        this.usedSize++;

        double loadFactor = loadFactor();
        if(loadFactor >= 0.75){
            resize();
        }
    }

    private double loadFactor(){
        return this.usedSize * 1.0 / this.array.length;
    }

    private void resize(){
        int size = 2 * this.array.length;
        Node[] tmpArr = new Node[size];

        for(int i = 0; i < array.length; i++){
            Node cur = array[i];
            while(cur != null){
                Node curNext = cur.next;
                int newIndex = cur.key % size;

                cur.next = tmpArr[newIndex];
                tmpArr[newIndex] = cur;
                cur = curNext;
            }
        }

        this.array = tmpArr;
    }

    public int get(int key){
        int index = key % this.array.length;
        Node cur = this.array[index];
        while(cur != null){
            if(cur.key == key){
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }
}
